﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using devtm.Editor.TypeSelector;

namespace devtm.AutoMapper.CustomCode.TemplateAdd
{
    public partial class AddMapRoot : Form
    {
        public AddMapRoot()
        {

            InitializeComponent();

            SourceList.DataBindings.Add("SelectedItem", this, "SourceType");
            TargetList.DataBindings.Add("SelectedItem", this, "TargetType");

        }

        private List<Type> list;
        internal void SetList(List<Type> lst)
        {

            list = lst;

            SourceList.DisplayMember = "FullName";
            SourceList.ValueMember = "AssemblyQualifiedName";

            TargetList.DisplayMember = "FullName";
            TargetList.ValueMember = "AssemblyQualifiedName";

            LoadListes();

        }

        private bool _loadliste = false;
        private void LoadListes()
        {

            SourceList.Sorted = true;
            TargetList.Sorted = true;

            _loadliste = true;
            if (DontshowAbstractType.Checked)
            {
                SourceList.DataSource = list.Where(c => !c.IsAbstract && !c.IsInterface).ToList();
                TargetList.DataSource = list.Where(c => !c.IsAbstract && !c.IsInterface).ToList();
            }
            else
            {
                SourceList.DataSource = list.ToList();
                TargetList.DataSource = list.ToList();
            }

            _loadliste = false;

            if (!string.IsNullOrEmpty(_source))            
                foreach (Type item in SourceList.DataSource as IEnumerable)
                    if (item.FullName == _source)
                    {
                        SourceList.SelectedItem = item;
                        break;
                    }
                
            

            if (!string.IsNullOrEmpty(_target))            
                foreach (Type item in TargetList.DataSource as IEnumerable)
                    if (item.FullName == _target)
                    {
                        TargetList.SelectedItem = item;
                        break;
                    }
                
            

        }

        private void button1_Click(object sender, EventArgs e)
        {

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void FindType(ComboBox list, Type type)
        {

            string t = list.Text;

            foreach (Type item in list.Items)
            {
                if (item.FullName == t)
                {
                    list.SelectedItem = item;
                    return;
                }
            }

            type = null;

            button1.Enabled = (SourceType != null && TargetType != null);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void SourceList_SelectedIndexChanged(object sender, EventArgs e)
        {

            button1.Enabled = (SourceType != null && TargetType != null);

            if (_loadliste)
                return;

            if (SourceList.SelectedItem != null)
                _source = (SourceList.SelectedItem as Type).FullName;

            button1.Enabled = (SourceType != null && TargetType != null);

        }

        private void TargetList_SelectedIndexChanged(object sender, EventArgs e)
        {

            button1.Enabled = (SourceType != null && TargetType != null);

            if (_loadliste)
                return;

            if (TargetList.SelectedItem != null)
                _target = (TargetList.SelectedItem as Type).FullName;

            button1.Enabled = (SourceType != null && TargetType != null);

        }


        public Type SourceType { get { return (Type)SourceList.SelectedItem; } }
        public Type TargetType { get { return (Type)TargetList.SelectedItem; } }


        public bool CreateBack { get { return GenerateBack.Checked; } }

        private static string _source = string.Empty;
        private static string _target = string.Empty;


        private void AddMapRoot_Load(object sender, EventArgs e)
        {

        }

        private void GenerateBack_CheckedChanged(object sender, EventArgs e)
        {
            LoadListes();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            LoadListes();
        }

        private void TargetList_TextChanged(object sender, EventArgs e)
        {

            FindType(TargetList, TargetType);

            button1.Enabled = (SourceType != null && TargetType != null);

        }

        private void SourceList_TextChanged(object sender, EventArgs e)
        {

            FindType(TargetList, TargetType);

            button1.Enabled = (SourceType != null && TargetType != null);

        }


    }
}
